<template>
  <div>
    <el-card shadow="never" class="ivu-mt" :body-style="{padding:0}">
        <div class="padding-add">
           <el-form
             ref="curlFrom"
             :model="from"
             :label-width="labelWidth"
             :label-position="labelPosition"
             inline
             @submit.native.prevent
           >
        <el-form-item label="用户名称：" label-for="uid">
            <el-input
                v-model="from.uid"
                placeholder="请输入用户名称"
                class="form_content_width"
            />
        </el-form-item>

        <el-form-item label="关联商品：" label-for="product_id">
            <el-input
                v-model="from.product_id"
                placeholder="请输入关联商品"
                class="form_content_width"
            />
        </el-form-item>

        <el-form-item label="房间编号：" label-for="room_id">
            <el-input
                v-model="from.room_id"
                placeholder="请输入房间编号"
                class="form_content_width"
            />
        </el-form-item>

<el-form-item label="过期时间:">
    <el-date-picker
        :editable="false"
        clearabl
        @change="searchs"
        v-model="from.expire_time"
        format="yyyy/MM/dd"
        type="daterange"
        value-format="yyyy/MM/dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width:250px;"
    ></el-date-picker>
</el-form-item>

<el-form-item label="添加时间:">
    <el-date-picker
        :editable="false"
        clearabl
        @change="searchs"
        v-model="from.add_time"
        format="yyyy/MM/dd"
        type="daterange"
        value-format="yyyy/MM/dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width:250px;"
    ></el-date-picker>
</el-form-item>

              <el-form-item>
                 <el-button type="primary" @click="searchs">查询</el-button>
              </el-form-item>
           </el-form>
        </div>
    </el-card>

    <el-card shadow="never" dis-hover class="ivu-mt mt16">
      <!-- <el-row type="flex">
        <el-col v-bind="grid">
          <el-button v-auth="['user_pack-add']" type="primary" icon="md-add" @click="add">添加</el-button>
        </el-col>
      </el-row> -->
      <el-table
          :data="dataList"
          ref="table"
          class="mt25"
          :loading="loading"
          highlight-current-row
      >
        <el-table-column prop="uid" label="用户名称">
		   <template slot-scope="scope">
			  <router-link :to="{ path: $routeProStr + '/user/list?uid='+scope.row.uid }">
				{{scope.row.nickname}}
			  </router-link>
		  </template>
        </el-table-column>
        <el-table-column prop="product_id" label="关联商品">
		   <template slot-scope="scope">
			  <router-link :to="{ path: $routeProStr + '/product/product_list?store_name='+scope.row.store_name }">
				{{scope.row.store_name}}
			  </router-link>
		  </template>
        </el-table-column>
        <el-table-column prop="room_id" label="房间编号">
		   <template slot-scope="scope">
			  <router-link :to="{ path: $routeProStr + '/crud/wwj_room?room_id='+scope.row.room_id }">
				{{scope.row.room_id}}
			  </router-link>
		  </template>
        </el-table-column>
        <el-table-column prop="expire_time" label="过期时间">
            <template slot-scope="scope">
				{{scope.row.expire_time | timeFormat }}
            </template>
        </el-table-column>
        <el-table-column prop="add_time" label="添加时间">
            <template slot-scope="scope">
				{{scope.row.add_time | timeFormat }}
            </template>
        </el-table-column>
        <!-- <el-table-column prop="status" label="背包状态">
            <template slot-scope="scope">
				{{scope.row.status | convertStatus}}
            </template>
        </el-table-column> -->
        <el-table-column label="操作">
            <template slot-scope="scope">
              <a @click="show(scope.row)">详情</a>
<!--              <el-divider direction="vertical" />
              <a @click="edit(scope.row.id)">修改</a> -->
              <el-divider direction="vertical" />
              <a @click="del(scope.row, '删除', scope.$index)">删除</a>
            </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <pagination :total="total"  @pagination="pageChange"  :limit.sync="from.limit" :page.sync="from.page" />
      </div>
    </el-card>

    <el-dialog title="查看详情" :visible.sync="dialogTableVisible" v-if='dialogTableVisible'>
        <el-descriptions title="用户娃娃机中奖背包详情">
            <el-descriptions-item label="背包ID">{{info.id}}</el-descriptions-item>
            <el-descriptions-item label="用户名称">
				<router-link :to="{ path: $routeProStr + '/user/list?uid='+info.uid }">
					{{info.nickname}}
				</router-link>		
			</el-descriptions-item>
            <el-descriptions-item label="商品名称">
				<router-link :to="{ path: $routeProStr + '/product/product_list?store_name='+info.store_name }">
					{{info.store_name}}
				</router-link>
			</el-descriptions-item>
            <el-descriptions-item label="娃娃机房间编号">
				<router-link :to="{ path: $routeProStr + '/crud/wwj_room?room_id='+info.room_id }">
					{{info.room_id}}
				</router-link>		
			</el-descriptions-item>
            <el-descriptions-item label="过期时间">{{info.expire_time | timeFormat }}</el-descriptions-item>
            <el-descriptions-item label="添加时间">{{info.add_time | timeFormat }}</el-descriptions-item>
            <!-- <el-descriptions-item label="可兑换积分（后续用）">{{info.integral}}</el-descriptions-item> -->
            <!-- <el-descriptions-item label="是否删除">{{info.is_del}}</el-descriptions-item> -->
            <el-descriptions-item label="背包状态">{{info.status | convertStatus}}</el-descriptions-item>

        </el-descriptions>
    </el-dialog>

  </div>
</template>

<script>
import dayjs from 'dayjs';
import { mapState } from 'vuex';
import { userPackSaveApi, userPackStatusApi, userPackDeleteApi, userPackUpdateApi, getUserPackCreateApi, getUserPackEditApi, getUserPackListApi, getUserPackReadApi} from '@/api/crud/userPack';
export default {
  name: 'user_pack',
  data() {
    return {
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      from: {
        uid:'',
        product_id:'',
        room_id:'',
        expire_time:'',
        add_time:'',
        page: 1,
        limit: 15,
      },
      dataList: [],
      total: 0,
      dialogTableVisible: false,
      info: {},
    };
  },
  filters: {
    timeFormat(value, birthday) {
      let i = birthday ? 'YYYY-MM-DD' : 'YYYY-MM-DD HH:mm:ss';
      if (!value) {
        return '-';
      }
      return dayjs(value * 1000).format(i);
    },
    convertStatus(value) {
      if (value == 1) {
        return '可用';
      } else if (value == 0) {
        return '不可用';
      } else {
        return '未知';
      }
    },
  },
  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : '75px';
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'left';
    },
  },
  created() {
    this.getList();
  },
  methods: {
    show(row) {
        getUserPackReadApi(row.id).then(res => {
           this.dialogTableVisible = true;
           this.info = res.data;
        }).catch(res => {
          this.$Message.error(res.msg);
        })
    },
    //修改状态
    updateStatus(row, field) {
        userPackStatusApi(row.id, {field: field, value: row[field]})
        .then(async (res) => {
          this.$message.success(res.msg);
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
    // 添加
    add() {
      this.$modalForm(getUserPackCreateApi()).then(() => this.getList());
    },
    // 表格搜索
    searchs() {
      this.from.page = 1;
      this.getList();
    },
    //列表
    getList() {
      this.loading = true;
      getUserPackListApi(this.from)
          .then(async (res) => {
            let data = res.data;
            this.dataList = data.list;
            this.total = data.count;
            this.loading = false;
          })
          .catch((res) => {
            this.loading = false;
            this.$Message.error(res.msg);
          });
    },
    //分页
    pageChange(index) {
      this.from.page = index;
      this.getList();
    },
    // 修改
    edit(id) {
      this.$modalForm(getUserPackEditApi(id)).then(() => this.getList());
    },
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `crud/user_pack/${row.id}`,
        method: 'DELETE',
        ids: '',
      };
      this.$modalSure(delfromData)
          .then((res) => {
            this.$Message.success(res.msg);
            this.getList();
          })
          .catch((res) => {
            this.$Message.error(res.msg);
          });
    },
  },
};
</script>

<style scoped lang="stylus"></style>
